package com.dmgis.qxfw.dataservice.businessservice.common.util;


import com.dmgis.entity.DmGridInfo;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.My3DPoint;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.WindPoint;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.GridDataPicAndXY;
import com.dmgis.weblib.GridAnalysis;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Grid文件处理
 *
 * @author dsq
 */
public class GridUtil {
    /**
     * 解析文本类型格点
     *
     * @param context
     * @return
     */
    public static ArrayList<My3DPoint> ReadGridText(String context) {
        ArrayList<My3DPoint> pList = new ArrayList<>();
        try {
            if (context.length() > 0) {
                String[] params = context.split(";");
                String[] gridInfo = params[0].split(",");//获取格点配置信息
                String[] zValues = params[2].split(",");//获取格点值
                double xmin = Double.valueOf(gridInfo[0]);
                double ymin = Double.valueOf(gridInfo[1]);
                double xmax = Double.valueOf(gridInfo[2]);
                double ymax = Double.valueOf(gridInfo[3]);
                int row = Integer.valueOf(params[1].split(",")[0]);
                int col = Integer.valueOf(params[1].split(",")[1]);
                double rowSpan = (ymax - ymin) / (row + 1);
                double colSpan = (xmax - xmin) / (col + 1);
                for (int i = 0; i < row; i++)
                    for (int j = 0; j < col; j++) {
                        double x = xmin + j * colSpan;
                        double y = ymin + i * rowSpan;
                        double z = Double.valueOf(zValues[i * row + col]);
                        My3DPoint point = new My3DPoint(x, y, z);
                        pList.add(point);
                    }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return pList;
    }

    /**
     * 1、解析格点文件
     * 2、使用前需要判断格点是否存在
     *
     * @param filePath 文件路径
     * @return
     */
    public static ArrayList<My3DPoint> ReadGridFile(String filePath) {
        ArrayList<My3DPoint> pList = new ArrayList<>();
        try {
            File gridFile = new File(filePath);
            if (gridFile.exists()) {
                GridAnalysis GridAnalysis = new GridAnalysis();
                GridAnalysis.openFile(filePath);
                DmGridInfo gridInfo = GridAnalysis.getGridInfo();
                double xmin = gridInfo.getxMin();
                double ymin = gridInfo.getyMin();
                double xmax = gridInfo.getxMax();
                double ymax = gridInfo.getyMax();
                int row = gridInfo.getRows();
                int col = gridInfo.getCols();
                double rowSpan = (ymax - ymin) / (row + 1);
                double colSpan = (xmax - xmin) / (col + 1);
                for (int i = 0; i < row; i++)
                    for (int j = 0; j < col; j++) {
                        double x = xmin + j * colSpan;
                        double y = ymin + i * rowSpan;
                        double z = Double.parseDouble(String.format("%.1f",GridAnalysis.getValue(i, j)));
                        My3DPoint point = new My3DPoint(x, y, z);
                        pList.add(point);
                    }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return pList;
    }


    /**
     * 读取风格点
     *
     * @param windUContext U分量
     * @param windVContext V分量
     * @return
     */
    public static ArrayList<WindPoint> ReadWinGridText(String windUContext, String windVContext) {
        ArrayList<WindPoint> pList = new ArrayList<>();
        try {
            if (windUContext.length() > 0 && windVContext.length() > 0) {
                String[] params = windUContext.split(";");
                String[] gridInfo = params[0].split(",");//获取格点配置信息
                String[] windUValues = params[2].split(",");//U分量
                String[] windVValues = windVContext.split(";")[2].split(",");//V分量
                double xmin = Double.valueOf(gridInfo[0]);
                double ymin = Double.valueOf(gridInfo[1]);
                double xmax = Double.valueOf(gridInfo[2]);
                double ymax = Double.valueOf(gridInfo[3]);
                int row = Integer.valueOf(params[1].split(",")[0]);
                int col = Integer.valueOf(params[1].split(",")[1]);
                double rowSpan = (ymax - ymin) / (row + 1);
                double colSpan = (xmax - xmin) / (col + 1);
                for (int i = 0; i < row; i++)
                    for (int j = 0; j < col; j++) {
                        double x = xmin + j * colSpan;
                        double y = ymin + i * rowSpan;
                        double[] wind = UVConvertToWind(Double.valueOf(windUValues[i * row + col]), Double.valueOf(windVValues[i * row + col]));
                        WindPoint point = new WindPoint(x, y, wind[1], wind[0]);
                        pList.add(point);
                    }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return pList;
    }

    /**
     * 读取风格点文件
     *
     * @param windUFile U分量文件
     * @param windVFile V分量文件
     * @return
     */
    public static ArrayList<WindPoint> ReadWinGridFiles(String windUFile, String windVFile) {
        ArrayList<WindPoint> pList = new ArrayList<>();
        try {
            File uFile = new File(windUFile);
            File vFile = new File(windVFile);
            if (uFile.exists() && vFile.exists()) {
                GridAnalysis GridAnalysisU = new GridAnalysis();
                GridAnalysis GridAnalysisV = new GridAnalysis();
                GridAnalysisU.openFile(windUFile);
                GridAnalysisV.openFile(windVFile);
                DmGridInfo dmGridDataInfo = GridAnalysisU.getGridInfo();
                double xmin = dmGridDataInfo.getxMin();
                double ymin = dmGridDataInfo.getyMin();
                double xmax = dmGridDataInfo.getxMax();
                double ymax = dmGridDataInfo.getyMax();
                int row = dmGridDataInfo.getRows();
                int col = dmGridDataInfo.getCols();
                double rowSpan = (ymax - ymin) / (row + 1);
                double colSpan = (xmax - xmin) / (col + 1);
                for (int i = 0; i < row; i++)
                    for (int j = 0; j < col; j++) {
                        double x = xmin + j * colSpan;
                        double y = ymin + i * rowSpan;
                        double[] wind = UVConvertToWind(GridAnalysisU.getValue(i, j), GridAnalysisV.getValue(i, j));
                        WindPoint point = new WindPoint(x, y, wind[1], wind[0]);
                        pList.add(point);
                    }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return pList;
    }

    /**
     * UV转换成风速风向
     *
     * @param u U分量
     * @param v V分量
     * @return [0]表示风速，[1]表示风向
     */
    public static double[] UVConvertToWind(double u, double v) {
        double[] wind = new double[2];
        try {
            double windAngle = 0;
            double p = 180 / Math.PI;
            double k = Math.PI / 2; //90°对应的π值
            double windValue = Math.sqrt(u * u + v * v);
            if (v != 0) {
                if (u == 0)
                    windAngle = v > 0 ? 0 : 2 * k;
                if (u > 0)
                    windAngle = v > 0 ? (k - Math.atan(v / u)) : (k + Math.atan(-v / u));
                if (u < 0)
                    windAngle = v > 0 ? (3 * k + Math.atan(-v / u)) : (3 * k - Math.atan(v / u));
            } else {
                if (u > 0)
                    windAngle = k;
                if (u < 0)
                    windAngle = 3 * k;
                if (u == 0)
                    windAngle = 0;
            }

            windAngle = windAngle * p;
            if (windAngle > 180)
                windAngle = windAngle - 180;
            else
                windAngle = windAngle + 180;
            wind[0] = Double.parseDouble(String.format("%.1f",windValue));
            wind[1] = Double.parseDouble(String.format("%.0f",windAngle));
        } catch (Exception ex) {
            System.out.println("风UV转换失败：" + ex.getStackTrace());
        }
        return wind;
    }
}